package com.example.reactordemo.util;


import lombok.Data;

import java.io.Serializable;

/**
 * @ClassName: Res
 * @Description: 将数据封装返回给前端
 * @Author: jacky
 */
@Data
public class Res implements Serializable {

    private static final long serialVersionUID = 1L;

    private int code;

    private boolean success;

    private String message;

    private Object data = null;

    private long timestamp;

    public Res() {

    }

    public Res(boolean success, int code, String msg, Object data) {
        this.success = success;
        this.code = code;
        this.message = msg;
        this.data = data;
        this.timestamp = System.currentTimeMillis();
    }

    public static <T> Res successWrapper(T pojo, int code, String msg) {
        Res json = new Res(true, code, msg, pojo);
        return json;
    }

    public static <T> Res successWrapper(T data) {
        Res json = new Res(true, 200, null, data);
        return json;
    }

    public static Res successWrapper(Integer code, String msg) {
        Res json = new Res(true, code, msg, null);
        return json;
    }

    public static <T> Res successWrapper(Integer code, T data) {
        Res json = new Res(true, code, null, data);
        return json;
    }

    public static Res successWrapper(Integer code) {
        Res json = new Res(true, code, "Request.Success", null);
        return json;
    }

    public static <T> Res failureWrapper(T pojo, Integer code, String msg) {
        Res json = new Res(false, code, msg, pojo);
        return json;
    }

    public static Res failureWrapper(Integer code, String msg) {
        Res json = new Res(false, code, msg, null);
        return json;
    }
}
